//
//  UIView+OSHelp.m
//  UniversalApp
//
//  Created by 凯朱 on 2019/5/31.
//  Copyright © 2019 徐阳. All rights reserved.
//

#import "UIView+OSHelp.h"

@implementation UIView (OSHelp)
/**
 *  自动从xib创建视图
 */
+(instancetype)viewFromXIB{
    
    NSString *name = NSStringFromClass(self);
    
    UIView *xibView = [[[NSBundle mainBundle] loadNibNamed:name owner:nil options:nil] firstObject];
    
    return xibView;
}

-(void)setTextFieldPlaceholdarWithStr:(NSString *)placeholdar textColor:(UIColor *)textColor font:(UIFont *)font{
    UITextField *textField = (UITextField *)self;
    if (font == nil) {
        font = kFont(18);
    }
    if (textColor == nil) {
        textColor = OSColor_4;
    }
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:kSTRING(placeholdar) attributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:textColor}];
}

/*
 * shadowColor 阴影颜色
 *
 * shadowOpacity 阴影透明度，默认0
 *
 * shadowRadius  阴影半径，默认3
 *
 * shadowPathSide 设置哪一侧的阴影，
 
 * shadowPathWidth 阴影的宽度，
 
 * type 1-左上右上 2-全方位 3-左下右下
 
 */

-(void)LX_SetShadowPathWith:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowSide:(LXShadowPathSide)shadowPathSide shadowPathWidth:(CGFloat)shadowPathWidth radiusLocation:(NSInteger)type{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIRectCorner corner ;
        if (type == 1) {
            corner = UIRectCornerTopLeft|UIRectCornerTopRight;
        }else if (type == 2) {
            corner = UIRectCornerAllCorners;
        }else{
            corner = UIRectCornerBottomLeft|UIRectCornerBottomRight;
        }
        if (@available(iOS 11.0,*)) {
            self.layer.cornerRadius = shadowRadius;
            self.layer.maskedCorners = (CACornerMask)corner;
        }else{
            UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(shadowRadius, shadowRadius)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
            self.frame = self.bounds;
            maskLayer.path = path2.CGPath;
            self.layer.mask = maskLayer;
        }
        self.layer.masksToBounds = NO;
        self.clipsToBounds = NO;
        self.layer.shadowColor = shadowColor.CGColor;
        self.layer.shadowOpacity = shadowOpacity;
        //设置抗锯齿边缘
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.layer.shadowOffset = CGSizeZero;
        
        CGRect shadowRect;
        CGFloat originX = 0;
        CGFloat originY = 0;
        CGFloat originW = self.bounds.size.width;
        CGFloat originH = self.bounds.size.height;
        switch (shadowPathSide) {
            case LXShadowPathTop:
                shadowRect  = CGRectMake(originX, originY - shadowPathWidth/2, originW,  shadowPathWidth);
                break;
            case LXShadowPathBottom:
                shadowRect  = CGRectMake(originX, originH -shadowPathWidth/2, originW, shadowPathWidth);
                break;
            case LXShadowPathLeft:
                shadowRect  = CGRectMake(originX - shadowPathWidth/2, originY, shadowPathWidth, originH);
                break;
            case LXShadowPathRight:
                shadowRect  = CGRectMake(originW - shadowPathWidth/2, originY, shadowPathWidth, originH);
                break;
            case LXShadowPathNoTop:
                shadowRect  = CGRectMake(originX -shadowPathWidth/2, originY +1, originW +shadowPathWidth,originH + shadowPathWidth/2 );
                break;
            case LXShadowPathAllSide:
                shadowRect  = CGRectMake(originX - shadowPathWidth/2, originY - shadowPathWidth/2, originW +  shadowPathWidth, originH + shadowPathWidth);
                break;
        }
        UIBezierPath *path =[UIBezierPath bezierPathWithRect:shadowRect];
        self.layer.shadowPath = path.CGPath;
    });
}

/// 给view绘制虚线边框
/// @param lineWidth 虚线宽
/// @param lineColor 虚线颜色
- (void)drawDashedBorderWith:(CGFloat)lineWidth lineColor:(UIColor *)lineColor{
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = lineColor.CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    border.frame = self.bounds;
    border.lineWidth = lineWidth;
    border.lineCap = @"square";
    // 设置线宽和线间距
    border.lineDashPattern = @[@5, @5];
    [self.layer addSublayer:border];
}

/**
 设置任意圆角及边框(实线/虚线)
 
 @param corners 要设置的圆角位置集合
 @param radius 圆角半径
 @param lineWidth 边框宽度
 @param lineColor 边框颜色
 @param lineDashPattern 虚线集合
 */
- (void)rounderWithCorners:(UIRectCorner)corners radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth lineColor:(UIColor *_Nullable )lineColor dash:(NSArray<NSNumber *>*_Nullable )lineDashPattern{
    
    ///解决masonry布局获取不了正确的frame
    [self.superview layoutIfNeeded];
    
    //绘制圆/圆弧---适用于全圆
    //UIBezierPath *maskPath = [UIBezierPath bezierPathWithArcCenter:self.bounds radius:iconWidth*0.5 startAngle:0 endAngle:2*M_PI clockwise:YES];
    //矩形---圆角
    //UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.iconImage.width*0.5];
    //宽高相等为全圆，不等为椭圆
    //UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.lineWidth = lineWidth;
    borderLayer.strokeColor = lineColor.CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    if (lineDashPattern) {
        borderLayer.lineDashPattern = lineDashPattern;
        //borderLayer.lineCap = @"butt";
    }
    borderLayer.path = maskPath.CGPath;
    [self.layer insertSublayer:borderLayer atIndex:0];
    self.layer.mask = maskLayer;
}


/**
 设置任意圆角 --- 性能优化
 
 @param corners 要设置的圆角位置集合
 @param radius 圆角半径
 */
- (void)rounderWithCorners:(UIRectCorner)corners radius:(CGFloat)radius{
    
    [self.superview layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    
}

/**
 设置圆角 --- 性能优化
 
 @param radius 圆角半径
 */
- (void)roundCornerWithRadius:(CGFloat)radius{
    
    [self rounderWithCorners:UIRectCornerAllCorners radius:radius];
    
}


@end
